package com.ysm.smfu.food;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.activity_transition.ActivityTransitionLauncher;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.view.SpacesItemDecoration;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FoodPhotoActivity extends BaseActivity {


    String[] tabs = {"菜品", "环境", "其他", "全部"};
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    ArrayList<String> list;
    private MyPagerAdapter adapter;
    private ArrayList<Integer> heightList;

    @Override
    protected void init() {
        super.init();
        list = new ArrayList<>();
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        list.add("http://img13.poco.cn/mypoco/myphoto/20120828/15/55689209201208281549023849547194135_001.jpg");
        list.add("http://img13.poco.cn/mypoco/myphoto/20120828/15/55689209201208281549023849547194135_001.jpg");
        list.add("http://img13.poco.cn/mypoco/myphoto/20120828/15/55689209201208281549023849547194135_001.jpg");
        heightList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            heightList.add((int) (300 + Math.random() * 350));
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_food_photo);
        ButterKnife.bind(this);
    }

    @Override
    protected void initListener() {
        super.initListener();
        adapter = new MyPagerAdapter();
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return tabs.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            RecyclerView view = (RecyclerView) LayoutInflater.from(FoodPhotoActivity.this).inflate(R.layout.recyclerview, null);
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
            view.setLayoutManager(layoutManager);
            SpacesItemDecoration decoration = new SpacesItemDecoration(10);
            view.addItemDecoration(decoration);
            MyAdapter adapter = new MyAdapter(list);
            view.setAdapter(adapter);
            container.addView(view);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(FoodPhotoActivity.this, FoodLargeActivity.class);
                    intent.putExtra("position", position);
                    intent.putStringArrayListExtra("list", list);
                    ActivityTransitionLauncher.with(FoodPhotoActivity.this).from(view).launch(intent);
//                    startActivity(intent);
                }
            });
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }

    private class MyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public MyAdapter(List<String> data) {
            super(R.layout.imageview_card, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            int adapterPosition = helper.getAdapterPosition();
            ImageView view = helper.getView(R.id.img);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = heightList.get(adapterPosition);
            view.setLayoutParams(layoutParams);
            Glide.with(FoodPhotoActivity.this).load(item)
                    .placeholder(R.drawable.pay_businessicon_defualt)
                    .error(R.drawable.pay_businessicon_defualt)
                    .into(view);
        }
    }

//    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {
//        @Override
//        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            return new MyHolder(LayoutInflater.from(FoodPhotoActivity.this).inflate(R.layout.imageview_card, null));
//        }
//
//        @Override
//        public void onBindViewHolder(MyHolder holder, int position) {
//            holder.setData(list.get(position));
//        }
//
//        @Override
//        public int getItemCount() {
//            return 2;
//        }
//    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final ImageView img;

        public MyHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
        }

        public void setData(String url) {
            Glide.with(FoodPhotoActivity.this).load(url)
                    .placeholder(R.drawable.pay_businessicon_defualt)
                    .error(R.drawable.pay_businessicon_defualt)
                    .into(img);
        }
    }


    @OnClick(R.id.back)
    public void onclick(View view) {
        finish();
    }

}
